/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package generatorGraphics.graphics;

import grammaticalClasses.nwConsole.ShowStatDay;
import grammaticalClasses.nwConsole.ShowStatShort;
import grammaticalClasses.nwConsole.simpleClasses.PathStats;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import org.jdesktop.swingx.geom.Star2D;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.util.ShapeUtilities;

/**
 *
 * @author Heitor
 */
public class PowerXStab extends Graph{
    
    private ArrayList<String> parameterFilterDay = new ArrayList<>();
    private ArrayList<String> parameterFilter15to15 = new ArrayList<>();
    
    public PowerXStab(){
        name = "PowerXStab";
        help = "Help of Power versus Stability";
        
        configUnitOptions();
        commands.add("ShowStatNet");
        commands.add("ShowStatDay");
        commands.add("ShowStatCur");
        commands.add("ShowStatShort");
    }
    
    private void configUnitOptions(){
        
        unitList.add("Time per Day");
        unitList.add("Time 15 to 15");
    }
    
    @Override
    protected void createGraph(String unit) {
    
        int indice;
        switch (unit){
            case "Time per Day":
                indice = search.getAnswer(commands, "ShowStatDay");

                chartPanel = new ChartPanel(buildGraphDayFilter(null, (ArrayList<ShowStatDay>) answer.get(indice)));
                chartPanel.setBackground(Color.LIGHT_GRAY);
                chartPanel.setPreferredSize(new Dimension(700, 350)); 
                chartPanel.setDomainZoomable(false);
                chartPanel.setRangeZoomable(false);
                break;
            case "Time 15 to 15":
                indice = search.getAnswer(commands, "ShowStatShort");
                
                chartPanel = new ChartPanel(buildGraph15to15Filter(null, (ArrayList<ShowStatShort>) answer.get(indice)));
                chartPanel.setBackground(Color.LIGHT_GRAY);
                chartPanel.setPreferredSize(new Dimension(700, 350)); 
                chartPanel.setDomainZoomable(false);
                chartPanel.setRangeZoomable(false);
                break;
            default:
                chartPanel = new ChartPanel(null);
                break;
        }
    }

    private JFreeChart buildGraphDayFilter(Object parameterFiltrate, ArrayList<ShowStatDay> auxStatDay) {
        
//        parameterFilterDay = new ArrayList<>();
        
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        XYSeries series = new XYSeries("Original");
        
        for(int i=0; i< auxStatDay.size(); i++){
            for(int j=0; j< auxStatDay.get(i).getPathStats().size(); j++){
                PathStats aux = auxStatDay.get(i).getPathStats().get(j);
                if(aux.getAbTx() != null){
                    if(aux.getAbTx() == 0){
                        if((aux.getBaTx() != null)&&(aux.getBaTx() != 0)){
                            //System.out.println(aux.getBaRSSI()+"&&"+ aux.getStab());
                            series.add(aux.getBaRSSI(), aux.getStab());
                        }
                    } else{
                        //System.out.println(aux.getAbRSSI()+"&&"+ aux.getStab());
                        series.add(aux.getAbRSSI(), aux.getStab());
                    }
                }
            }
        }
        xySeriesCollection.addSeries(series);
        
        return buildGraphDay(xySeriesCollection, auxStatDay);
    }

    private JFreeChart buildGraph15to15Filter(Object parameterFiltrate, ArrayList<ShowStatShort> auxStatShort) {
        
        XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        XYSeries series = new XYSeries("Original");
        
        for(int i=0; i< auxStatShort.size(); i++){
            for(int j=0; j< auxStatShort.get(i).getPathStats().size(); j++){
                PathStats aux = auxStatShort.get(i).getPathStats().get(j);
                if(aux.getAbTx() != null){
                    if(aux.getAbTx() == 0){
                        if((aux.getBaTx() != null)&&(aux.getBaTx() != 0)){
                            //System.out.println(aux.getBaRSSI()+"&&"+ aux.getStab());
                            series.add(aux.getBaRSSI(), aux.getStab());
                        }
                    } else{
                        //System.out.println(aux.getAbRSSI()+"&&"+ aux.getStab());
                        series.add(aux.getAbRSSI(), aux.getStab());
                    }
                }
            }
        }
        xySeriesCollection.addSeries(series);
        
        return buildGraph15to15(xySeriesCollection, auxStatShort);
    }

    private JFreeChart buildGraphDay(XYSeriesCollection xySeriesCollection, ArrayList<ShowStatDay> auxStatDay) {
        
        JFreeChart jfreechart = ChartFactory.createScatterPlot(
            "Power versus Stability", "Power", "Stability", xySeriesCollection,
            PlotOrientation.VERTICAL, false, true, false);
        Shape triangle = ShapeUtilities.createDownTriangle(2);
        Shape cross = ShapeUtilities.createDiagonalCross(2, 1);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        XYItemRenderer renderer = xyPlot.getRenderer();
        renderer.setSeriesPaint(0, Color.blue);
        renderer.setSeriesShape(0, cross);
        //renderer.setSeriesShape(0, );
        //adjustAxis((NumberAxis) xyPlot.getDomainAxis(), true);
        //adjustAxis((NumberAxis) xyPlot.getRangeAxis(), false);
        xyPlot.setBackgroundPaint(Color.white);
        jfreechart.setBackgroundPaint(new Color(238, 238, 238));
        return jfreechart;
    }
    
    private JFreeChart buildGraph15to15(XYSeriesCollection xySeriesCollection, ArrayList<ShowStatShort> auxStatShort){
        
        JFreeChart jfreechart = ChartFactory.createScatterPlot(
            "Power versus Stability", "Power", "Stability", xySeriesCollection,
            PlotOrientation.VERTICAL, false, true, false);
        Shape triangle = ShapeUtilities.createDownTriangle(2);
        Shape cross = ShapeUtilities.createDiagonalCross(2, 1);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        XYItemRenderer renderer = xyPlot.getRenderer();
        renderer.setSeriesPaint(0, Color.blue);
        renderer.setSeriesShape(0, triangle);
        //renderer.setSeriesShape(0, );
        //adjustAxis((NumberAxis) xyPlot.getDomainAxis(), true);
        //adjustAxis((NumberAxis) xyPlot.getRangeAxis(), false);
        xyPlot.setBackgroundPaint(Color.white);
        jfreechart.setBackgroundPaint(new Color(238, 238, 238));
        return jfreechart;
        
    }
    
}
